<?php 
namespace App\Dao;


class BaseDao
{
    
    protected $models;
    protected $res;
    public function __construct(){
        $this->models = static::setModel();
    }

    public function getOne($where, $select = "*",$order=[]){
        
        if(count($order) == 2)  return  $this->formatData($this->models::where($where)->orderBy($order[0],$order[1])->select($select)->first());

        return $this->formatData($this->models::where($where)->select($select)->first());
    }

    public function getListIn($where = [], $whereIn = [], $select = "*"){
        return $this->formatData($this->models::where($where)->when(count($whereIn) > 0, function($query) use ($whereIn){
            $query->whereIn($whereIn[0], $whereIn[1]);
        })->select($select)->get());

    }
    // 显示所有数据
    public function getList($where, $select = "*"){
        return $this->formatData($this->models::where($where)->select($select)->get());
    }

    // 显示分页
    public function getListPage($where, $limit, $select = "*"){
        return $this->formatData($this->models::where($where)->select($select)
            ->paginate($limit));
    }

    // 获取总数
    public function count($where){
        return $this->models::where($where)->count();
    }
    //插入单条数据
    public  function create($arr){
       return  $this->models::create($arr)->id;
    }

    // 插入多条数据
    public function insert($arr){
        return  $this->models::insert($arr);
    }
    // 删除
    public function delete($where){
        return $this->models::where($where)->delete();
    }
    //更新数据
    public  function update($where,$arr){
        return $this->models::where($where)->update($arr);
    }
    
    //自增数据
    public  function  setinc($where,$key,$num){
        return $this->models::where($where)->increment($key,$num);
    }
    
    //自减数据
    public  function  setdec($where,$key,$num){
        return $this->models::where($where)->decrement($key,$num);
    }
    

    public function formatData($res)
    {
        return $res ? $res->toArray() : [];
    }





}